package com.ce.pms.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ce.pms.charge.dao.IParameterDefDao;
import com.ce.pms.charge.model.ParameterDefDomain;
import com.ce.pms.report.dao.IReportDao;
import com.ce.pms.report.queryvo.OnticUpkeepBuildCollectQueryVO;
import com.ce.pms.report.vo.OnticUpkeepBuildCollectReportVO;
import com.ce.report.entity.CEReportDataSet;
import com.ce.report.exception.CEReportException;
import com.ce.report.service.ICEReportDataSetService;
import com.ce.report.vo.ReportsQueryBaseVo;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;

/**
 *
 * 功能描述：单项费用分栋汇总表(本体维修基金分栋汇总表）Service
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 黄彦军
 * Created on 2008-11-3
 * Edit author:
 * Edit on:
 * 修改原因:
 */
public class OnticUpkeepBuildCollectService implements ICEReportDataSetService {

	private IReportDao reportDao;

	private IParameterDefDao parameterDefDao;

	public CEReportDataSet getReportDataSet(ReportsQueryBaseVo arg) throws CEReportException {
		OnticUpkeepBuildCollectQueryVO queryVO = (OnticUpkeepBuildCollectQueryVO) arg;

		return new CEReportDataSet(getData(queryVO),getHeadInfo(queryVO));
	}

	/**
	 * 功能描述:获取列表数据  <br>
	 * @param queryVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-13  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<OnticUpkeepBuildCollectReportVO> getData(OnticUpkeepBuildCollectQueryVO queryVO){
		String  pkey = queryVO.getChargeItemPkey();
		String  precinct = queryVO.getPrecinct();
		String  period = DateUtil.convertDateToStr(queryVO.getComputeMonth(), "yyyyMM");

		Object[] obj = new Object[]{period,period,period,precinct,pkey,period,period};
		List<Object[]> temp = reportDao.getDataList("onticUpkeepBuildCollect", obj);

		List<OnticUpkeepBuildCollectReportVO> list = arrayToVO(temp);

		return list;
	}

	/**
	 * 功能描述:对象数组 转 vo数组  <br>
	 * @param temp
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private  List<OnticUpkeepBuildCollectReportVO> arrayToVO(List<Object []> temp){
		if(BlankUtil.isBlank(temp)){
			return null;
		}
		List<OnticUpkeepBuildCollectReportVO> list = new ArrayList<OnticUpkeepBuildCollectReportVO>();
		for (Object[] obj : temp) {
			OnticUpkeepBuildCollectReportVO reportVO = new OnticUpkeepBuildCollectReportVO();
			reportVO.setCompanyName((String)obj[0]);		//物业名称
			reportVO.setBuildingName((String)obj[2]);		//楼栋名称
			reportVO.setActualThis(setValue(obj[3]));		//实收本月
			reportVO.setActualBefore(setValue(obj[4]));	//实收往月
			reportVO.setLatefee(setValue(obj[5]));		//滞纳金

			BigDecimal actualPay = new BigDecimal(0);   //本月实收合计为：实收本月 + 实收往月 + 滞纳金
			if(!BlankUtil.isBlank(reportVO.getActualThis())){
				actualPay = actualPay.add(reportVO.getActualThis());
			}
			if(!BlankUtil.isBlank(reportVO.getActualBefore())){
				actualPay = actualPay.add(reportVO.getActualBefore());
			}
			if(!BlankUtil.isBlank(reportVO.getLatefee())){
				actualPay = actualPay.add(reportVO.getLatefee());
			}
			reportVO.setActualPay(actualPay);   //实收合计

			list.add(reportVO);
		}

		return list;
	}

	//	传入的值必须是 BigDecimal 类型的,否则返回空如果为空或值为0 则返回空
	private BigDecimal setValue(Object obj){
		if (obj instanceof BigDecimal) {
			if(((BigDecimal)obj).intValue() == 0){
				return null;
			}
			return (BigDecimal)obj;
		}
		return null;
	}

	/**
	 * 功能描述:获取头信息  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-13  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Map<String, Object> getHeadInfo(OnticUpkeepBuildCollectQueryVO queryVO) {
		Map<String, Object> map = new HashMap<String, Object>();
		String computeMonth = DateUtil.convertDateToStr(queryVO.getComputeMonth(), "yyyy年MM月");
		map.put("reportHeadStr", "计费月份:"+computeMonth);
		map.put("printDate", new Date());

		ParameterDefDomain defDomain = parameterDefDao.findUniqueBy("pkey", queryVO.getChargeItemPkey());
		String chargeItemName = "";
		if (!BlankUtil.isBlank(defDomain)){
			chargeItemName = defDomain.getPname();
		}
		map.put("chargeItemName", chargeItemName);//收费项目

		return map;
	}

	public void setReportDao(IReportDao reportDao) {
		this.reportDao = reportDao;
	}

	public void setParameterDefDao(IParameterDefDao parameterDefDao) {
		this.parameterDefDao = parameterDefDao;
	}

}
